package com.yc.happ.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yc.happ.entity.Admin;
import com.yc.happ.entity.Elder;
import com.yc.happ.entity.OrderItem;
import com.yc.happ.entity.OrderList;
import com.yc.happ.entity.Volunteer;
import com.yc.happ.service.BackService;
import com.yc.happ.service.ChooseService;
import com.yc.happ.service.ElderService;
import com.yc.happ.service.LoginService;
import com.yc.happ.service.RegisterService;
import com.yc.happ.service.VolunteerService;
import com.yc.happ.serviceImpl.BackServiceImpl;
import com.yc.happ.serviceImpl.ChooseServiceImpl;
import com.yc.happ.serviceImpl.ElderServiceImpl;
import com.yc.happ.serviceImpl.LoginServiceImpl;
import com.yc.happ.serviceImpl.RegisterServiceImpl;
import com.yc.happ.serviceImpl.VolunteerServiceImpl;
import com.yc.happ.util.DBHelper;

@WebServlet("/happinesshome/user/*")
public class VolunteerServlet extends BaseServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 7719011182576098805L;
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if("vregister".equals(getRequestFlag(request))){
			doRegister(request,response);
		}else if("eregister".equals(getRequestFlag(request))){
			doElderRegister(request,response);
		}else if("login".equals(getRequestFlag(request))){
			doLogin(request,response);
		}else if("update".equals(getRequestFlag(request))){
			doUpdate(request,response);
		}else if("list".equals(getRequestFlag(request))){
			doVolunteerList(request,response);
		}else if("choose".equals(getRequestFlag(request))){
			doChoose(request,response);
		}
	}

	private void doChoose(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String vid=request.getParameter("vid");
		Elder elder=(Elder)request.getSession().getAttribute("elder");
		String eid = null;
		String jsonStr=null;
		PrintWriter out=response.getWriter();
		if(elder==null){
			jsonStr="{\"success\":\"nologin\"}";
			out.print(jsonStr);
			out.flush();
			out.close();
		}else{
			eid=elder.getVid().toString();
		}
		if(elder.getIsChoose()==1){
			jsonStr="{\"success\":\"isChoose\"}";
			out.print(jsonStr);
			out.flush();
			out.close();
		}
		ChooseService chooseService=new ChooseServiceImpl();
		
		if(chooseService.getChoose(vid, eid)){
			jsonStr="{\"success\":\"true\"}";
			out.print(jsonStr);
			out.flush();
			out.close();
		}else{
			jsonStr="{\"success\":\"false\"}";
			out.print(jsonStr);
			out.flush();
			out.close();
		}
	}

	private void doUpdate(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String name=request.getParameter("name");
		String phone=request.getParameter("phone");
		String special=request.getParameter("special");
		String date=request.getParameter("date");
		String time1=request.getParameter("time1");
		String time2=request.getParameter("time2");
		
		Volunteer volunteer =(Volunteer)request.getSession().getAttribute("volunteer");
		String uname=volunteer.getVuname();
		
		VolunteerService volunteerService=new VolunteerServiceImpl();
		boolean b=volunteerService.update(name,phone,special,date,time1,time2,uname);

		String jsonStr=null;
		PrintWriter out=response.getWriter();
		if(b==true){
			jsonStr="{\"success\":true}";
			out.print(jsonStr);
			out.flush();
			out.close();
		}else{
			//失败时，在原页面
			jsonStr = "{\"success\":false}";
			out.print(jsonStr);
			out.flush();
			out.close();
		}
	}

	private void doLogin(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		
		String uname=request.getParameter("name").trim();
		String upwd=request.getParameter("pwd").trim();
		LoginService loginService=new LoginServiceImpl();
		Volunteer volunteer=loginService.volunteerLogin(uname, upwd);
		Elder elder=loginService.elderLogin(uname, upwd);
		String jsonStr=null;
		PrintWriter out=response.getWriter();
		if(volunteer!=null){
			request.getSession().removeAttribute("elder");
			request.getSession().setAttribute("volunteer",volunteer);
			jsonStr="{\"success\":\"volunteer\"}";
			out.print(jsonStr);
			out.flush();
			out.close();
		}else if(elder!=null){
			request.getSession().removeAttribute("volunteer");
			request.getSession().setAttribute("elder",elder);
			jsonStr="{\"success\":\"elder\"}";
			out.print(jsonStr);
			out.flush();
			out.close();
		}else if("admin".equals(uname)&&"1234".equals(upwd)){
			BackService back=new BackServiceImpl();
			List<OrderList> listOrderList=back.getAllOrderList(1);
			List<Elder> listelder=back.getAllElder(1);
			List<Volunteer> listVolunteer=back.getAllVolunteer(1);
			List<OrderItem> listOrderItem=back.getAllOrderItem(1);
			
			request.getSession().setAttribute("backorderlist", listOrderList);
			request.getSession().setAttribute("backelder", listelder);
			request.getSession().setAttribute("backvolunteer", listVolunteer);
			request.getSession().setAttribute("backorderitem", listOrderItem);
			
			Admin admin=new Admin();
			admin.setAname(uname);
			admin.setApassword("1234");
			request.getSession().setAttribute("admin",admin);
			jsonStr="{\"success\":\"admin\"}";
			out.print(jsonStr);
			out.flush();
			out.close();
		}else{
			jsonStr="{\"success\":\"error\"}";
			out.print(jsonStr);
			out.flush();
			out.close();
		}
	}

	private void doElderRegister(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String vuname=request.getParameter("vuname");
		String vpwd=request.getParameter("vpwd");
		String vsex=request.getParameter("vsex");
		String vaddress=request.getParameter("vaddress");
		String vaddress3=request.getParameter("vaddress3");
		RegisterService registerService=new RegisterServiceImpl();
		boolean b=registerService.elderRegiter(vuname, vpwd, vsex, vaddress, vaddress3);
		String jsonStr=null;
		PrintWriter out=response.getWriter();
		if(b==true){
			jsonStr="{\"success\":true}";
			out.print(jsonStr);
			out.flush();
			out.close();
		}else{
			//失败时，在原页面
			jsonStr = "{\"success\":false}";
			out.print(jsonStr);
			out.flush();
			out.close();
		}
	}
	
	private void doRegister(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		String vuname=request.getParameter("vuname");
		String vpwd=request.getParameter("vpwd");
		String vsex=request.getParameter("vsex");
		String vaddress=request.getParameter("vaddress");
		String vtype=request.getParameter("vtype");
		RegisterService registerService=new RegisterServiceImpl();
		boolean b=registerService.volunteerRegister(vuname, vpwd, vsex, vaddress, vtype);
		
		String jsonStr=null;
		PrintWriter out=response.getWriter();
		if(b==true){
			jsonStr="{\"success\":true}";
			out.print(jsonStr);
			out.flush();
			out.close();
		}else{
			//失败时，在原页面
			jsonStr = "{\"success\":false}";
			out.print(jsonStr);
			out.flush();
			out.close();
		}
	}
	private void doVolunteerList(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Elder elder=(Elder)request.getSession().getAttribute("elder");
		String jsonStr=null;
		PrintWriter out=response.getWriter();
		if(elder==null){
			jsonStr="{\"success\":\"nologin\"}";
			out.print(jsonStr);
			out.flush();
			out.close();
		}
		String type=request.getParameter("type");
		String address=request.getParameter("address");
		BackService backService=new BackServiceImpl();
		List<Volunteer> listVol=backService.getAllVolunteer(1);
		List<Volunteer> newVolList=new ArrayList<Volunteer>();

		for(Volunteer vol:listVol){
			if(vol.getVtype().equals(type)&&vol.getVaddress().equals(address)&&vol.getIsChoose()==0){
				
				newVolList.add(vol);
			}
		}
		request.getSession().setAttribute("volunteers",newVolList);
		if(type!=null&&address!=null){
			jsonStr="{\"success\":\"true\"}";
			out.print(jsonStr);
			out.flush();
			out.close();
		}else{
			jsonStr="{\"success\":\"false\"}";
			out.print(jsonStr);
			out.flush();
			out.close();
		}
	}
}
